<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'

export default {
  props: {
    className: {
      type: String,
      default: 'score-chart'
    },
    id: {
      type: String,
      default: 'score-chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    data: {
      type: Object,
      require: true
    },
    title: {
      type: String,
      default: '不同时间超重数及超重比'
    },
    queryTime: {
      type: String,
      default: '近24小时'
    }
  },
  data() {
    return {
      chart: null,
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', () => {
      this.chart.resize()
    })
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const that = this
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        backgroundColor: '#F3F9FF',
        title: [
          {
            text: that.title,
            textStyle: {
              fontSize: 18
            },
            left: 10,
            top: 10
          }
        ],
        graphic: [
          {
            type: 'text',
            left: 250,
            top: 10,
            style: {
              text: this.queryTime,
              textAlign: 'center',
              fontSize: 14
            }
          }
        ],
        legend: {
          left: 'center',
          top: 0
        },
        tooltip: {},
        color: ['#006EFF', '#F9AF00'],
        xAxis: {
          type: 'category'
          // data: ['00点', '01点', '02点', '03点', '04点', '05点', '06点', '07点', '08点', '09点', '10点', '11点', '12点', '13点', '14点', '15点', '16点', '17点', '18点', '19点', '20点', '21点', '22点', '23点']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '超重数',
            type: 'bar',
            barMaxWidth: 34,
            tooltip: {
              formatter: function (params) {
                return '超重数:' + params.value[1]
              }
            },
            data: that.data.count
          },
          {
            name: '超重比',
            type: 'line',
            tooltip: {
              formatter: function (params) {
                return '超重比:' + params.value[1] + '%'
              }
            },
            data: that.data.percent
          }
        ],
        toolbox: {
          // x: 'right',
          // y: 'bottom',
          right: 20,
          bottom: 0,
          feature: {
            saveAsImage: {}
          }
        }
      })
      window.addEventListener('resize', () => {
        if (this.chart) {
          this.chart.resize()
        }
      })
    }
  }
}
</script>
